﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace BackupObjects
{

    public class BackupRepository
    {
        public const string UserNameAdmin = "admin";
        
        BackupObjectsList _backups;

        /// <summary>
        /// Gets the list of backup object. 
        /// This property should be used only after LoadAll is called.
        /// </summary>
        public BackupObjectsList Backups { get; private set; }
        
        public BackupRepository()
        {
            Backups = new BackupObjectsList();
        }
        public BackupObject Load(Guid id, string User)
        {
            var obj = Backups.Load(id);
            switch (obj.Visibility)
            {
                case VisibilityBackup.AccesibleByGuid:
                case VisibilityBackup.AccesibleByName:
                    return obj;
                case VisibilityBackup.Private:
                    if (User == UserNameAdmin)
                    {
                        return obj;
                    }
                    else
                    {
                        return null;
                    }
                default:
                    throw new ArgumentException(" not found :" + obj.Visibility);
            }
        }
        public BackupObject Load(string Name, string User)
        {

            var obj = Backups.Load(Name);
            switch (obj.Visibility)
            {
                case VisibilityBackup.AccesibleByGuid:
                case VisibilityBackup.AccesibleByName:
                    return obj;
                case VisibilityBackup.Private:
                    if (User == UserNameAdmin)
                    {
                        return obj;
                    }
                    else
                    {
                        return null;
                    }
                default:
                    throw new ArgumentException(" not found :" + obj.Visibility);
            }

        }
        /// <summary>
        /// load all backups for the user in the backups public variable
        /// </summary>
        /// <param name="User">user name  - must be admin in the current version</param>
        public void LoadAll(string User)
        {
            if (User != UserNameAdmin)
                return;

            Backups.LoadAll();

        }

        public void Save(BackupObject bo)
        {
            bo.UploadDate = DateTime.Now;
            Backups.Save(bo);
         
            BackupTagRepository backupObjectTagRepository=new BackupTagRepository();
            backupObjectTagRepository.Save(bo);
        }

        public void Delete(Guid id, string User)
        {

            //retrieve object to see permissions 
            //TODO  :  put rights to user
            var obj = Load(id, User);
            if (obj == null)
            {
                //TODO: log user trying to delete
                return;
            }
            Backups.Delete(id);

            BackupTagRepository backupObjectTagRepository = new BackupTagRepository();
            backupObjectTagRepository.DeleteByBackupObjectId(id);

        }
        public Stream GetBackupStreamIco(Guid id, string User)
        {
            try
            {
                //retrieve object to see permissions 
                //TODO  :  put rights to user
                var obj = Load(id, User);
                if (obj == null)
                    return null;
                //TODO: find occurences of .ico and replace it

                return Backups.GetBackupStream(id.ToString(), id.ToString() + "ico");

            }
            catch (StorageClientException ex)
            {
                //does not exists
                //TODO : log
                return null;
            }

        }
        public Stream GetBackupStream(Guid id, string User)
        {
            //retrieve object to see permissions 
            //TODO  :  put rights to user
            var obj = Load(id, User);
            if (obj == null)
                return null;
            return Backups.GetBackupStream(id);

        }

        public void Delete(BackupObject backupObject, string username)
        {
            Delete(backupObject.ID, username);
        }

        /// <summary>
        /// Get all the backup entities that have the given tags.
        /// </summary>
        /// <param name="tagids">The tag list.</param>
        /// <returns>The list of entities with the tag ids.</returns>
        public List<BackupObject> GetBackupEntitiesByTags(List<Guid> tagIds)
        {
            return Backups.GetBackupEntitiesByTags(tagIds);
        }

        /// <summary>
        /// Get all backup entities that with a given tag id.
        /// </summary>
        /// <param name="tagId">The tag id.</param>
        /// <returns>The collection of backup entities with the given tag id.</returns>
        public List<BackupObject> GetBackupEntitiesByTag(Guid tagId)
        {
            return Backups.GetBackupEntitiesByTag(tagId);
        }
    }
}